﻿/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Laurent Berthelot
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.draw.shapes
{
	import flash.display.Graphics;
	import flash.geom.Rectangle;
	import marcel.display.draw.props.CornerProps;
	import marcel.display.draw.props.FillProps;
	import marcel.display.draw.props.LineProps;
	

	/**
	* The RectangleShape class contains a static method that you can use to draw a rectangle shape.
	* @author 	Laurent Berthelot
	*/
	public class RectangleShape extends BasicShape
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Non-instanciable class, only statics methods in RectangleShape class.
		 */
		public function RectangleShape(o:ConstructorAccess) {}
		
		//--------------------------------------------------------------------------
		//
		//  Public static methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Draws a rectangle.
		 * @param	graphics	Graphics object to draw on.
		 * @param	rec			Rectangle size.
		 * @param	fp			Fill properties.
		 * @param	lp			Line style properties.
		 * @param	cp			Corner properties.
		 */
		public static function drawShape(graphics:Graphics, rec:Rectangle, lp:LineProps, fp:FillProps, cp:CornerProps):Graphics {
			_beginFill(graphics, fp, rec);
			_lineStyle(graphics, lp);
			if (cp === null) {
				graphics.drawRect(rec.x, rec.y, rec.width, rec.height);
			} else {
				var ellipseWidth:Number = Math.min(cp.ellipseWidth, rec.width);
				var ellipseHeight:Number = Math.min(cp.ellipseHeight, rec.height);
				if (cp.rounded) {
					graphics.drawRoundRect(rec.x, rec.y, rec.width, rec.height, ellipseWidth, ellipseHeight);
				} else {
					graphics.moveTo( rec.x , rec.y + ellipseHeight);
					graphics.lineTo( rec.x, rec.y + rec.height - ellipseHeight );
					graphics.lineTo( rec.x + ellipseWidth, rec.y + rec.height );
					graphics.lineTo( rec.x + rec.width - ellipseWidth, rec.y + rec.height );
					graphics.lineTo( rec.x + rec.width, rec.y + rec.height - ellipseHeight );
					graphics.lineTo( rec.x + rec.width, rec.y + ellipseHeight );
					graphics.lineTo( rec.x + rec.width - ellipseWidth, rec.y );
					graphics.lineTo( rec.x + ellipseWidth, rec.y );
					graphics.lineTo( rec.x , rec.y + ellipseHeight );
				}
			}
			_endFill(graphics);
			return graphics;
		}
	}
}
internal class ConstructorAccess {}